rm update
[EroBeats.git] / Djinn and Tonic - Erobeats / TempoTools.cpp
blob45fb8afcb08343bae7fc48053a63e0c939e8e66c
1 #include "TempoTools.h"
3 using namespace std::chrono;
5 TempoTools::TempoTools(RhythmIO* rhythmio)
7 rio = rhythmio;
9 errorMarginPerf = 16;
10 errorMarginGood = 32;
11 errorMarginOK = 48;
12 errorMarginBad = 64;
14 long long dummyTime = (duration_cast<milliseconds>(system_clock::now().time_since_epoch())).count();
15 for (int i = 0; i < 10; i++) {
16 oldTime[i] = dummyTime;
17 fileTime[i] = dummyTime;
21 TempoTools::~TempoTools()
25 void TempoTools::createTimeData(std::string levelPath) {
26 clearTimeData();
27 timeKeys = rio->createBeatPatterns(levelPath);
29 void TempoTools::clearTimeData() {
30 timeKeys.clear();
31 hitData.clear();
32 cueData.clear();
33 rio->keyPositions = 0;
35 void TempoTools::proccessDataSlow() {
36 rio->bufferBeatData(timeKeys, musicStart, &hitData, &cueData);
38 void TempoTools::setMusicStart(long long mStart) {
39 musicStart = mStart;
42 void TempoTools::countDown(float* time, float duration) {
43 milliseconds ms = duration_cast<milliseconds>(
44 system_clock::now().time_since_epoch());
45 long long expiredTime = ms.count();
46 *time = duration - (expiredTime - musicStart) / 1000.0;
49 double TempoTools::clickTempo(int time) {
50 int limit = 3;
51 clickTimes.push_back(time);
52 int size = clickTimes.size();
54 if (size == 1) {
55 return 1;
57 else if (size > limit) {
58 clickTimes.erase(clickTimes.begin());
59 size = limit;
61 double combined = 0;
62 for (int t = 0; t < size - 1; t++) {
63 combined += clickTimes.at(t + 1) - clickTimes.at(t);
65 //time seperation
66 double combinedAv = combined / (size - 1);
67 //CPM
68 double clickRate = 60000 / combinedAv;
69 double factor = clickRate / tempo;
70 if (combinedAv < 1) {
71 return 1;
73 else {
74 return factor;
79 short TempoTools::autoPlay(int type){
80 milliseconds ms = duration_cast<milliseconds>(
81 system_clock::now().time_since_epoch());
82 long long time = ms.count();
83 long long difference = 0;
84 for (int i = 1; i < 10; i++) {
85 if (type == i * 10) {
86 difference = cueFileTime[i] - time;
87 //if(type == 10) std::cout << (difference) << "\n";
88 if (difference < 16 && difference > -16 && !autoplayed[i]) {
89 autoplayed[i] = true;
90 return 1;
92 else if (difference < 16 && difference > -16 && autoplayed[i]) {}
93 else {
94 autoplayed[i] = false;
98 return 0;
101 short TempoTools::clickAccuracy(int type, bool cout) {
102 milliseconds ms = duration_cast<milliseconds>(
103 system_clock::now().time_since_epoch());
105 long long clickTime = ms.count();
106 short hit;
107 long long difference;
108 long long oldDifference;
109 for (int i = 1; i < 10; i++) {
110 if (type == i) {
111 //Ahead
112 difference = std::abs(fileTime[i] - clickTime);
113 //Behind
114 oldDifference = std::abs(oldTime[i] - clickTime);
117 if (cout) {
118 std::cout << "\n ----\t" << (oldDifference) << " ---- \n"
119 << "\n ----\t" << difference << " ---- \n";
122 if (difference < errorMarginOK || oldDifference < errorMarginOK) {
123 if (cout) std::cout << "OK\n";
124 return 0;
126 else if (difference > errorMarginOK){
127 if (cout) std::cout << "Ahead\n";
128 return 1;
129 //ahead
131 else if (oldDifference > errorMarginOK) {
132 if (cout)std::cout << "Behind\n";
133 return -1;
134 //behind
136 else return 9;
138 short TempoTools::clickAccuracyRange(int type) {
139 milliseconds ms = duration_cast<milliseconds>(
140 system_clock::now().time_since_epoch());
142 long long clickTime = ms.count();
143 short hit;
144 long long difference;
145 long long oldDifference;
146 for (int i = 1; i < 10; i++) {
147 if (type == i) {
148 //Ahead
149 difference = std::abs(fileTime[i] - clickTime);
150 //Behind
151 oldDifference = std::abs(oldTime[i] - clickTime);
154 std::cout << "\n ----\t" << (oldDifference) << " ---- \n"
155 << "\n ----\t" << difference << " ---- \n";
156 //signs are flipped for some reason.
157 if (difference < errorMarginPerf || oldDifference < errorMarginPerf) {
158 std::cout << "A";
159 return 0;
161 else if (difference < errorMarginGood || oldDifference < errorMarginGood) {
162 std::cout << "B";
163 return 1;
164 //ahead
166 else if (difference < errorMarginOK || oldDifference < errorMarginOK) {
167 std::cout << "C";
168 return 2;
169 //ahead
171 else if (difference > errorMarginOK || oldDifference > errorMarginOK) {
172 std::cout << "F";
173 return 3;
174 //ahead
176 else return 9;
179 //when a certain vector's time has expired, switch to the next one and keep the old one in a long long.
180 void TempoTools::moveTicker() {
181 milliseconds ms = duration_cast<milliseconds>(
182 system_clock::now().time_since_epoch());
183 long long ct = ms.count();
185 for (int i = 1; i < 10; i++) {
186 if (hitData.size() != 0) {
187 if (ct > fileTime[i] && hitData.at(0).at(1) == i) {
188 oldTime[i] = fileTime[i];
189 fileTime[i] = hitData.at(0).at(0);
190 hitData.erase(hitData.begin());
191 std::cout << "\tupdate Hit " << hitData.size() << "\n";
195 for (int i = 1; i < 10; i++) {
196 if (cueData.size() != 0) {
197 if (ct > cueFileTime[i] && cueData.at(0).at(1) == i * 10) {
198 cueFileTime[i] = cueData.at(0).at(0);
199 cueData.erase(cueData.begin());
200 std::cout << "\tupdate Cue" << cueData.size() << "\n";
203 else {
204 done = true;